package org.myOrg.spetrum.GUI;

import java.util.Iterator;
import java.util.Map.Entry;

import org.myOrg.spetrum.Driver.SpectrumUnion;
import org.myOrg.spetrum.combination.Tuple;
import org.myOrg.spetrum.io.InputManager;
import org.myOrg.spetrum.spectrumStruct.Spectrum;
import org.myOrg.spetrum.spectrumStruct.SpectrumFactory;
import org.myOrg.spetrum.util.Message;
import org.myOrg.spetrum.util.MessageMangement;
import org.myOrg.spetrum.util.MessageMangementImplement;
import org.myOrg.spetrum.util.OverallFactory;
import org.myOrg.spetrum.util.Reciver;

public class BugModelShowPanel extends ShowPanel implements Reciver {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private int showBugLength = 0;

	public BugModelShowPanel() {
		this.register(MessageMangementImplement.RUNOVER,
				OverallFactory.getMessageManger());
		this.register(MessageMangementImplement.READOVER,
				OverallFactory.getMessageManger());
	}

	@Override
	public void handleMessage(Message msg) {
		// TODO Auto-generated method stub
		if (msg.getType() == MessageMangementImplement.RUNOVER) {
			this.clear();
			if(msg.getInfo() instanceof Spectrum){
				Spectrum spectrum = (Spectrum)msg.getInfo() ;
				showBugNum(spectrum);
				showSpectrumBugModel(spectrum);
			}else{
				SpectrumUnion specrums = (SpectrumUnion)msg.getInfo();
				Iterator<Entry<Integer,Spectrum>> itr = specrums.itertor();
				while(itr.hasNext()){
					Entry<Integer, Spectrum> entry = itr.next();
					showAlogrithm(entry.getKey().intValue());
					showBugNum(entry.getValue());		
				}
				itr = specrums.itertor();
				while(itr.hasNext()){
					Entry<Integer, Spectrum> entry = itr.next();
					showAlogrithm(entry.getKey().intValue());
					showSpectrumBugModel(entry.getValue());
				}
			}
		} else if (msg.getType() == MessageMangementImplement.READOVER) {
			InputManager input = (InputManager) msg.getInfo();
			this.showBugLength = input.getParam().length;
			if (input.isFormatPass()) {
				this.clear();
			}
		}
	}
	private void showAlogrithm(int type){
		this.insertOneLine(SpectrumFactory.getName(type),
				StyleSet.getStyleTitle());
	}
	private void showBugNum(Spectrum spectrum){
		this.insertOneLine("all:" + spectrum.getBugs().size(), StyleSet.getStyleConsleRight());
	}
	private void showSpectrumBugModel(Spectrum spectrum) {
		this.insertOneLine("the bug model:", StyleSet.getStyleConsleRight());
		Iterator<Tuple> itr = spectrum.getBugs().iterator();
		while (itr.hasNext()) {
			this.insertOneLine(itr.next().toString(showBugLength),
					StyleSet.getStyleMode());
		}
	}

	@Override
	public void register(int messageType, MessageMangement messageManger) {
		// TODO Auto-generated method stub
		messageManger.register(this, messageType);
	}

}
